package com.school.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.school.pojo.Account;
import com.school.pojo.File;
import com.school.pojo.Student;
import com.school.pojo.Student;
import com.school.service.AccountService;
import com.school.service.FileService;
import com.school.service.SposService;
import com.school.service.StudentService;
import com.school.util.Result;
import com.school.util.Tool;

@RequestMapping("/student")
@RestController
public class StudentController implements Tool<Student> {

	@Autowired
	private StudentService studentService;

	@Autowired
	private AccountService accountService;
	
	@Autowired
	private FileService fileService;

	@Override
	@RequestMapping("/findAll")
	public Result<Student> findAll(int page, int pageSize, Student t) {

		List<Student> list = studentService.findAll(page, pageSize, t);
		System.out.println(list);
		PageInfo<Student> info = new PageInfo<>(list);
		Result<Student> result = new Result<Student>();
		result.setList(info.getList());
		result.setCount(info.getTotal());
		return result;
	}

	@Override
	@RequestMapping("/findById")
	public Result<Student> findbyId(int student_id) {
		Student Student= studentService.findbyId(student_id);
		
		Account account= accountService.findById(Student.getA_id());
		File file= fileService.findbyId(Student.getFile_id());
		
		Student.setUsername(account.getUsername());
		Student.setPassword(account.getPassword());
		Student.setUrl(file.getUrl());
		Student.setStatus(account.getStatus());
		
		Result<Student> result=new Result<Student>();
		result.setData(Student);
		System.out.println(result);
		return result;

	}

	@Override
	@RequestMapping("/add")
	public int add(Student student) {
		System.out.println(student);
		// 添加账号
		int i = accountService.add(student);
		if (i != 0) {
			// 添加用户信息
			return studentService.add(student);
		}

		return 0;

	}

	@Override
	@RequestMapping("/edit")
	public int edit(Student t) {
		System.out.println(t);
		//修改学生用户内容
				int a=studentService.edit(t);
				//修改图片
				int b=fileService.edit(t);
				//修改密码
				int c=accountService.edit(t);
				
				if(a!=0&&b!=0&&c!=0) {
					return 1;
				}
				return 0;
	}
	
	@RequestMapping("/edits")
	public int edits(Student t) {
		System.out.println(t);
		//修改学生用户内容
		int a=studentService.edits(t);
		//修改图片
		int b=fileService.edit(t);
		//修改密码
		int c=accountService.edit(t);
		System.out.println(a);
		System.out.println(b);
		System.out.println(c);
		if(a!=0&&b!=0&&c!=0) {
			return 1;
		}
		return 0;
	}

	@Override
	public int del(int id) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int delbatch(int[] id) {
		
		return 0;
	}
	
	@RequestMapping("/delbatch")
	public int delbatch(@RequestParam(name="id[]")int[] id,@RequestParam(name="aid[]")int[] aid) {
		//假删老师信息
		int a=studentService.delbatch(id);
		//锁定账号
		int b=accountService.delbatch(aid);
		
		if(a!=0&&b!=0) {
			return 1;
		}
		return 0;
	}
	
	@RequestMapping("/findbyIds")
	public Result<Student> findbyIds(HttpServletRequest request,HttpServletResponse response){
		
		Account account=(Account)request.getSession().getAttribute("account");
		
		Student student= studentService.findByAId2(account.getAccount_id());
		student.setPassword(account.getUsername());
		student.setPassword(account.getPassword());
		
		Result<Student> result=new Result<>();
		result.setData(student);
		return result;
	}

}
